<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\DB;

/**
 * App\Models\GoldSplit
 *
 * @property int $id
 * @property int $user_id
 * @property int $gold_seed_id
 * @property int $gold_round_id
 * @property int $gold_product_id
 * @property int $product_id
 * @property int $product_sku_id
 * @property int $split_record_id
 * @property int $settled_record_id
 * @property int $round
 * @property string|null $gold_seed_title
 * @property string|null $product_title
 * @property string|null $sku_name
 * @property string $split_price
 * @property string|null $settled_at settle up split record
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\GoldProduct|null $goldProduct
 * @property-read \App\Models\GoldRecord|null $settledRecord
 * @property-read \App\Models\GoldRecord|null $splitRecord
 * @method static \Database\Factories\GoldSplitFactory factory(...$parameters)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit query()
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereGoldProductId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereGoldRoundId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereGoldSeedId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereGoldSeedTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereProductId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereProductSkuId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereProductTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereRound($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereSettledAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereSettledRecordId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereSkuName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereSplitPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereSplitRecordId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereUserId($value)
 * @mixin \Eloquent
 * @property int $new_gold_product_id
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereNewGoldProductId($value)
 * @property string $split_profit
 * @property string $split_beans
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereSplitBeans($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereSplitProfit($value)
 * @property \Illuminate\Support\Carbon|null $started_at start selling
 * @property-read \App\Models\User|null $user
 * @method static \Illuminate\Database\Eloquent\Builder|GoldSplit whereStartedAt($value)
 * @property-read \App\Models\GoldRound|null $goldRound
 * @property-read \App\Models\GoldSeed|null $goldSeed
 */
class GoldSplit extends Model
{
    use HasFactory;

    protected $guarded = ['id'];

    protected $dates = [
        'started_at',
        'settled_at',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function goldRound(): BelongsTo
    {
        return $this->belongsTo(GoldRound::class);
    }

    public function goldSeed(): BelongsTo
    {
        return $this->belongsTo(GoldSeed::class);
    }

    public function goldProduct(): BelongsTo
    {
        return $this->belongsTo(GoldProduct::class);
    }

    public function splitRecord(): BelongsTo
    {
        return $this->belongsTo(GoldRecord::class, 'split_record_id', 'id');
    }

    public function settledRecord(): BelongsTo
    {
        return $this->belongsTo(GoldRecord::class, 'settled_record_id', 'id');
    }

    public function settle(GoldRecord $record)
    {
        DB::transaction(function () use ($record) {
            $price = $this->split_price;
            $beans = $this->split_beans;
            $profit = $this->split_profit;
            $data = ['up_gold_record_id' => $this->split_record_id, 'gold_record_id' => $record->id];
            /*account_coins($this->user)->plusResellingCoins($price, $data);
            account_coins($this->user)->plusResellingBeansCoins($beans, $data);
            account_coins($this->user)->plusResellingProfitCoins($profit, $data);*/
            $this->update(['settled_at' => Carbon::now()]);
        });
    }
}
